package com.qf.service.impl;

import com.qf.mapper.ClazzMapper;
import com.qf.pojo.Clazz;
import com.qf.service.ClazzService;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    ClazzMapper clazzMapper;

    /**
     * 查询所有班级
     * @return
     */
    @Override
    public ResultMsg selectAll(Integer page,Integer limit) {
        if (page!=null){
            page=page<=0?1:page;
        }
        List<Clazz> clazzes=clazzMapper.selectAll(page==null?1:(page-1)*limit,limit);
        if (clazzes!=null){
            //查询所有行数，为分页准备，行数存在属性的ID里
            Clazz clazz = clazzMapper.selectAllRows();
            return new ResultMsg(0,"查询成功",clazzes,clazz.getClazzId());
        }else {
            return new ResultMsg(214,"查询失败",null,0);
        }
    }

    /**
     * 查询一个班级
     * @param clazzId
     * @return
     */
    @Override
    public ResultMsg selectOne(Integer clazzId) {
       Clazz clazz = clazzMapper.selectOne(clazzId);
       if (clazz!=null){
           return new ResultMsg(0,"查询成功",clazz,1);
       }else {
           return new ResultMsg(215,"查询失败",null,0);
       }
    }

    /**
     * 修改一个班级信息
     * @param clazz
     * @return
     */
    @Override
    public ResultMsg changeClazz(Clazz clazz) {
        int i = clazzMapper.changeClazz(clazz);
        if (i==0){
            return new ResultMsg(216,"修改失败",null,0);
        }else {
            Clazz clazz1 = clazzMapper.selectOne(clazz.getClazzId());
            return new ResultMsg(0,"修改成功",clazz1,0);
        }
    }

    /**
     * 删除一个班级
     * @param clazzId
     * @return
     */
    @Override
    public ResultMsg deleteOne(Integer clazzId) {
        int i = clazzMapper.deleteOne(clazzId);
        if (i==0){
            return new ResultMsg(218,"删除失败",null,0);
        }else {
            return new ResultMsg(0,"删除成功",null,0);
        }
    }

    /**
     * 新增一个班级
     * @param clazz
     * @return
     */
    @Override
    public ResultMsg addClazz(Clazz clazz) {
        int i = clazzMapper.addClazz(clazz);
        if (i==0){
            return new ResultMsg(220,"新增失败",null,0);
        }else {
            return new ResultMsg(0,"新增成功",clazz,1);
        }
    }

    /**
     * 批量删除班级
     * @param clazzId
     * @return
     */
    @Override
    public ResultMsg deleteClazz(List<Integer> clazzId) {
        int i = clazzMapper.deleteClazz(clazzId);
        if (i!=0){
            return new ResultMsg(0,"删除成功，删除了"+i+"个",null,0);
        }else {
            return new ResultMsg(211,"删除失败",null,0);
        }
    }

    /**
     * 根据姓名和状态模糊查询
     * @param name
     * @param status
     * @return
     */
    @Override
    public ResultMsg find(String name, String status) {
        int cstatus = -1;
        Clazz clazz = new Clazz();
        if (status!=null&&!"".equals(status)){
            if (status.equals("1")){
                cstatus=1;
            }else if (status.equals("0")){
                cstatus=0;
            }
            clazz.setClazzName(name);
            clazz.setClazzStatus(cstatus);
        }else {
            clazz.setClazzName(name);
        }
       List<Clazz> clazzes = clazzMapper.find(clazz);
        if (clazzes!=null){
            return new ResultMsg(0,"查询成功",clazzes,clazzes.size());
        }else {
            return new ResultMsg(219,"查询失败",null,0);
        }
    }

    /**
     * 修改班级状态
     * @param clazzId
     * @return
     */
    @Override
    public ResultMsg changeClazzStatus(Integer clazzId) {
        Clazz clazz = clazzMapper.selectOne(clazzId);
        int status = -1;
        if (clazz.getClazzStatus()==0){
            status=1;
        }else if (clazz.getClazzStatus()==1){
            status=0;
        }
       int i = clazzMapper.changeClazzStatus(clazzId,status);
        if (i!=0){
            return new ResultMsg(0,"修改成功",null,0);
        }else {
            return new ResultMsg(211,"修改失败",null,0);
        }
    }

}
